#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from datetime import datetime
from fastapi import APIRouter, Depends
from utils.response import SuccessResponse
from core.dependencies import IdList
from sqlalchemy.orm import joinedload
from core.exception import CustomException
from apps.base.auth.validate import AllUserAuth, Auth
from . import schemas, crud, params, models

app = APIRouter()


@app.get("/asr", summary="获取ASR表单")
async def get_asr(
        p: params.ASRParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.ASRDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.ASRSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/asr", summary="创建ASR表单")
async def create_asr(
        data: schemas.ASRIn,
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await crud.ASRDal(auth.db).create_data(data))


@app.put("/asr/{data_id}", summary="更新ASR表单")
async def put_asr(
        data_id: int,
        data: schemas.ASRIn,
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await crud.ASRDal(auth.db).put_data(data_id, data=data))


@app.put("/asr/{data_id}/stamp", summary="ASR表单计划或检验员刷卡")
async def put_asr_stamp(
        data_id: int,
        data: schemas.ASRStampType,
        auth: Auth = Depends(AllUserAuth())
):
    obj = await crud.ASRDal(auth.db).get_data(data_id=data_id)
    if data.type == "plan_user":
        obj.plan_user = auth.user.user_str_time
    elif data.type == "insp_user":
        obj.insp_user = auth.user.user_str_time
    await crud.ASRDal(auth.db).flush(obj)
    return SuccessResponse("更新成功")


@app.put("/asr/{data_id}/status", summary="更新ASR表单状态")
async def put_asr_status(
        data_id: int,
        data: schemas.ASRUpdateStatus,
        auth: Auth = Depends(AllUserAuth())

):
    obj = await crud.ASRDal(auth.db).get_data(data_id=data_id)
    if obj.status == "归档":
        raise CustomException("归档ASR不允许更新状态")
    if data.status and data.status == '作废':
        obj.status = '作废'
    else:
        if obj.status == "计划中":
            obj.status = '检验中'
        elif obj.status == '检验中':
            obj.status = '审核中'
        elif obj.status == '审核中':
            obj.status = '归档'
            
    await crud.ASRDal(auth.db).flush(obj)
    return SuccessResponse("更新成功")

@app.put("/asr/{data_id}/erratum", summary="添加勘误说明")
async def put_asr_erratum(
        data_id: int,
        data: schemas.ASRUpdateErratum,
        auth: Auth = Depends(AllUserAuth())
):
    data.erratum_datetime = datetime.now()
    data.erratum_user = auth.user.user_str
    await crud.ASRDal(auth.db).put_data(data_id=data_id, data=data)
    return SuccessResponse("更新成功")

@app.get("/asr/{data_id}", summary="获取ASR表单详情")
async def get_asr_detail(
        data_id: int,
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await crud.ASRDal(auth.db).get_data(data_id=data_id, v_schema=schemas.ASROut, v_options=[joinedload(models.DCASR.contents)]))


@app.delete("/asr", summary="批量删除ASR表单", description="软删除")
async def delete_asr(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.ASRDal(auth.db).delete_datas(ids=ids.ids, v_soft=True, delete_user=auth.user.user_str)
    return SuccessResponse("删除成功")


@app.delete("/asr/content", summary="删除ASR检验内容", description="软删除")
async def delete_asr_content(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.ASRContentDal(auth.db).delete_datas(ids=ids.ids, v_soft=True, delete_user=auth.user.user_str)
    return SuccessResponse("删除成功")


@app.put("/asr/content/stamps", summary="ASR检验内容刷卡", description="刷卡用户")
async def put_asr_content_stamps(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.ASRContentDal(auth.db).put_datas(ids=ids.ids, insp_stamp=auth.user.user_str_time)
    return SuccessResponse("更新成功")